﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm
{
    public partial class FormConfig : Form
    {
        string webAPI = ConfigurationManager.AppSettings["WebAPI"];
        public FormConfig()
        {
            StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void button_server_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox_server.Text))
            {
                Utils.SysMsg("服务器不能为空！");
            }
            else
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.AppSettings.Settings["WebAPI"].Value = textBox_server.Text;
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                    webAPI = ConfigurationManager.AppSettings["WebAPI"];
                    if (MessageBox.Show("配置成功，需要重启软件才能生效，是否现在重启？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        Application.Restart();
                        //开启新的实例
                        //System.Diagnostics.Process.Start(Application.ExecutablePath);
                        //关闭当前实例
                        System.Diagnostics.Process.GetCurrentProcess().Kill();
                    }
                }
                catch (Exception ex)
                {
                    Utils.SysMsg(ex.Message, MessageBoxIcon.Error);
                }
            }
            
        }

        private void FormConfig_Load(object sender, EventArgs e)
        {
            textBox_server.Text = webAPI;
        }
    }
}
